import { ConfigEnv, UserConfig, defineConfig, loadEnv } from 'vite'
import createVitePlugins from './vite/plugins'
import path from 'path'

export default defineConfig(({ mode, command }: ConfigEnv): UserConfig => {
  const env: Record<string, string> = loadEnv(mode, process.cwd())
  return {
    base: env.VITE_APP_CONTEXT_PATH,
    plugins: createVitePlugins(env, command === 'build'),
    resolve: {
      // ref: https://cn.vitejs.dev/config/#resolve-alias
      alias: {
        // set path for alias '~'
        '~': path.resolve(__dirname, './'),
        // set path for alias '@'
        '@': path.resolve(__dirname, './src')
      },
      // ref: https://cn.vitejs.dev/config/#resolve-extensions
      extensions: ['.mjs', '.js', '.ts', '.jsx', '.tsx', '.json', '.vue']
    },
    // server related config
    server: {
      port: Number(env.VITE_APP_PORT),
      host: true,
      open: false,
      proxy: {
        // ref: https://cn.vitejs.dev/config/#server-proxy
        [env.VITE_APP_BASE_API]: {
          // target: 'https://3265is8820.zicp.fun',
          // target:'http://192.168.123.138:8081',
          target:'http://47.108.86.86:8080',
          // target:'http://127.0.0.1:8080',
          changeOrigin: true,
          secure: false,
          rewrite: (path) => path.replace(new RegExp('^' + env.VITE_APP_BASE_API), '')
        }, // base api
        // [env.VITE_ALY_BASE_API]: {
        //   // target: 'https://3265is8820.zicp.fun',
        //   target:'https://fanzhiedu.oss-cn-chengdu.aliyuncs.com',
        //   // target:'https://4q8420u630.zicp.fun',
        //   changeOrigin: true,
        //   secure: true,
        //   rewrite: (path) => path.replace(new RegExp('^' + env.VITE_ALY_BASE_API), '')
        // } // aliyun api
      } // proxy
    }, //server
    build: {
      sourcemap: true,
    },
    //fix:error:stdin>:7356:1: warning: "@charset" must be the first rule in the file
    css: {
      postcss: {
        plugins: [
          {
            postcssPlugin: 'internal:charset-removal',
            AtRule: {
              charset: (atRule) => {
                if (atRule.name === 'charset') {
                  atRule.remove();
                }
              } // charset
            } // AtRule
          }
        ] // plugins
      } // postcss
    }, // css
    // precompile
    optimizeDeps: {
      include: [
        // vue
        'vue',
        'vue-router',
        'pinia',

        // axios
        'axios',

        // element-plus
        'element-plus/es/components/form/style/css',
        'element-plus/es/components/form-item/style/css',
        'element-plus/es/components/button/style/css',
        'element-plus/es/components/input/style/css',
        'element-plus/es/components/input-number/style/css',
        'element-plus/es/components/switch/style/css',
        'element-plus/es/components/upload/style/css',
        'element-plus/es/components/menu/style/css',
        'element-plus/es/components/col/style/css',
        'element-plus/es/components/icon/style/css',
        'element-plus/es/components/row/style/css',
        'element-plus/es/components/tag/style/css',
        'element-plus/es/components/dialog/style/css',
        'element-plus/es/components/loading/style/css',
        'element-plus/es/components/radio/style/css',
        'element-plus/es/components/radio-group/style/css',
        'element-plus/es/components/popover/style/css',
        'element-plus/es/components/scrollbar/style/css',
        'element-plus/es/components/tooltip/style/css',
        'element-plus/es/components/dropdown/style/css',
        'element-plus/es/components/dropdown-menu/style/css',
        'element-plus/es/components/dropdown-item/style/css',
        'element-plus/es/components/sub-menu/style/css',
        'element-plus/es/components/menu-item/style/css',
        'element-plus/es/components/divider/style/css',
        'element-plus/es/components/card/style/css',
        'element-plus/es/components/link/style/css',
        'element-plus/es/components/breadcrumb/style/css',
        'element-plus/es/components/breadcrumb-item/style/css',
        'element-plus/es/components/table/style/css',
        'element-plus/es/components/tree-select/style/css',
        'element-plus/es/components/table-column/style/css',
        'element-plus/es/components/select/style/css',
        'element-plus/es/components/option/style/css',
        'element-plus/es/components/pagination/style/css',
        'element-plus/es/components/tree/style/css',
        'element-plus/es/components/alert/style/css',
        'element-plus/es/components/checkbox/style/css',
        'element-plus/es/components/date-picker/style/css',
        'element-plus/es/components/transfer/style/css',
        'element-plus/es/components/tabs/style/css',
        'element-plus/es/components/image/style/css',
        'element-plus/es/components/tab-pane/style/css'
      ]
    }
  }
})

